package com.hyt.it.ogt.data.service.pj.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.data.Constants;
import com.hyt.it.ogt.data.enums.CommonEnums;
import com.hyt.it.ogt.data.mapper.pj.PjDataPieceMapper;
import com.hyt.it.ogt.data.model.entity.pj.Paper;
import com.hyt.it.ogt.data.model.entity.pj.PaperItem;
import com.hyt.it.ogt.data.model.entity.pj.Piece;
import com.hyt.it.ogt.data.model.entity.pj.PieceItem;
import com.hyt.it.ogt.data.model.entity.pj.Subject;
import com.hyt.it.ogt.data.model.vo.pj.PaperDataVO;
import com.hyt.it.ogt.data.service.pj.IPjDataConfigService;
import com.hyt.it.ogt.data.service.pj.IPjDataPaperItemService;
import com.hyt.it.ogt.data.service.pj.IPjDataPaperService;
import com.hyt.it.ogt.data.service.pj.IPjDataPieceItemService;
import com.hyt.it.ogt.data.service.pj.IPjDataPieceService;
import com.hyt.it.ogt.pj.model.entity.Config;
import com.hyt.it.ogt.pj.utils.MathUtils;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p>
 * 评卷块表 服务实现类
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@Slf4j
@Service
public class PjDataPieceServiceImpl extends BaseServiceImpl<PjDataPieceMapper, Piece> implements IPjDataPieceService {

    @Autowired
    private PjDataPieceMapper pjDataPieceMapper;

    @Resource
    private IPjDataConfigService iPjDataConfigService;

    @Resource
    private IPjDataPaperService iPjDataPaperService;

    @Autowired
    private IPjDataPaperItemService iPjDataPaperItemService;

    @Resource
    private IPjDataPieceItemService pieceItemService;

    @Override
    public List<Piece> selectBySubject(String subjectId) {
        return pjDataPieceMapper.selectBySubject(subjectId);
    }

    @Override
    public List<Piece> selectByProjectIdSubjectId(String projectId, String subjectId) {
        return pjDataPieceMapper.selectByProjectIdSubjectId(projectId, subjectId);
    }

    @Override
    public Piece selectPaperPiece(String projectId, String subjectId, String paperId) {
        return pjDataPieceMapper.selectPaperPiece(projectId, subjectId, paperId);
    }

    @Override
    public void updateFinishStatus(String projectId, String subjectId, String pieceCode, Boolean status) {
        pjDataPieceMapper.updateFinishStatus(projectId, subjectId, pieceCode, status);
    }

    /**
     * 初始化评卷考试的piece信息
     * 
     * @see com.hyt.it.ogt.data.service.pj.IPjDataPieceService#initPiece(java.lang.String, java.util.List, java.util.List)
     **/
    @Override
    public void initPiece(String projectId, List<Subject> subjects) {
        // 生成piece、pieceItem、task、teacherTaskInfo
        int subjectOrderNum = 0;
        Config scoreStep = iPjDataConfigService.selectByLabel(projectId, Constants.MARK_SCORE_STEP);
        Config reMarkLeng = iPjDataConfigService.selectByLabel(projectId, Constants.MARK_REMARK_LENGTH);
        Config minSubmitTime = iPjDataConfigService.selectByLabel(projectId, Constants.MARK_MIN_SUBMIT_TIME);
        log.info("[推送评卷] markPaperIncrment subjects:{}", FastJsonUtil.getBeanToJson(subjects));
        for (Subject subject : subjects) {
            String subjectId = subject.getSubjectId();
            List<Piece> existPieces = selectByProjectIdSubjectId(projectId, subjectId);
            log.info("[推送评卷] markPaperIncrment existPieces:{}", FastJsonUtil.getBeanToJson(existPieces));
            String subjectPieceId = "";
            if (CollUtil.isEmpty(existPieces)) {
                subjectOrderNum++;
                Piece piece = new Piece();
                piece.setId(UUIDUtils.newSortUUID());
                piece.setProjectId(projectId);
                piece.setSubjectId(subjectId);
                piece.setParentId("0");
                piece.setPieceName(subject.getSubjectName());
                piece.setPieceType(CommonEnums.PIECE_TYPE_1.getCode());// 科目评卷块
                piece.setOrderNum(subjectOrderNum);
                piece.setScoreStep(MathUtils.parseBigDecimal(scoreStep));
                save(piece);
                subjectPieceId = piece.getId();
            } else {
                for (Piece piece : existPieces) {
                    if (piece.getPieceType() == 1) {
                        subjectPieceId = piece.getId();
                    }
                }
            }
            List<Paper> papers = iPjDataPaperService.selectParersBySubjectId(projectId, subjectId);
            log.info("[推送评卷] markPaperIncrment papers:{}", FastJsonUtil.getBeanToJson(papers));
            int paperPieceOrderNum = 0;
            String lastPackageId = null;
            for (Paper paper : papers) {
                Piece existePaperPiece = selectPaperPiece(projectId, subjectId, paper.getId());
                if (existePaperPiece == null) {
                    if (paper.getPackageId() == null || !paper.getPackageId().equals(lastPackageId)) {
                        // 同一个试卷包的paperPieceOrderNum相同
                        paperPieceOrderNum++;
                        lastPackageId = paper.getPackageId();
                    }
                    Piece existPiecePaper = selectPaperPiece(projectId, subjectId, paper.getId());
                    if (existPiecePaper == null) {
                        if (paper.getPackageId() == null || !paper.getPackageId().equals(lastPackageId)) {
                            // 同一个试卷包的paperPieceOrderNum相同
                            paperPieceOrderNum++;
                            lastPackageId = paper.getPackageId();
                        }
                        String paperId = paper.getId();
                        Piece piecePaper = new Piece();
                        piecePaper.setId(UUIDUtils.newSortUUID());
                        piecePaper.setProjectId(projectId);
                        piecePaper.setSubjectId(subjectId);
                        piecePaper.setParentId(subjectPieceId);
                        piecePaper.setPieceName(paper.getPaperName());
                        piecePaper.setPackageId(paper.getPackageId());
                        piecePaper.setPackageVersion(paper.getPackageVersion());
                        piecePaper.setSort(paper.getSort());
                        piecePaper.setPaperId(paperId);
                        // 试卷评卷块
                        piecePaper.setPieceType(CommonEnums.PIECE_TYPE_2.getCode());
                        piecePaper.setRejudgeLength(MathUtils.parseInt(reMarkLeng));
                        piecePaper.setMinSubmitTime(MathUtils.parseInt(minSubmitTime));
                        piecePaper.setOrderNum(paperPieceOrderNum);
                        piecePaper.setFullScore(paper.getSubjectiveScore());
                        piecePaper.setScoreStep(MathUtils.parseBigDecimal(scoreStep));
                        save(piecePaper);
                        List<PaperItem> paperItems = iPjDataPaperItemService.selectByPaperId(paperId);
                        List<PieceItem> pieceItems = new ArrayList<>();
                        for (PaperItem paperItem : paperItems) {
                            PieceItem pieceItem = new PieceItem();
                            pieceItem.setId(UUIDUtils.newSortUUID());
                            pieceItem.setItemId(paperItem.getTopicId());
                            pieceItem.setPieceId(piecePaper.getId());
                            pieceItems.add(pieceItem);
                        }
                        pieceItemService.saveBatch(pieceItems);
                    }
                }
            }
        }
    }
}
